<div class="content fixed-fluid">
    <div class="row-fluid">
        <div class="main-content" style="margin-top: 20px;">
            <link rel="stylesheet" href="/CSS/Desktop/select2.css"/>
            <script src="/JS/Desktop/select2.min.js" type="text/javascript" language="javascript"/>
            <h1 class="stream-title"><i class="icon-notifications-menu"></i><span>Emails &amp; Notifications</span></h1>
            <div class="clear"></div>
            <p class="notification-text">Changes here will not affect the alerts other users receive.</p>
            <h5 class="action-nav-title"><strong>SIGNALS EMAIL ALERTS</strong></h5>
            <div>

                <div class="email-alert-select">
                    <input id="daily_digest" class="email-notification-checkbox edit-notification" type="checkbox" <?php if (Options::getOption('daily_digest_email')) echo 'checked="checked"' ?>>
                    <label for="daily_digest">Daily Digest</label>
                    <div class="clear"></div>
                    <p class="help-block">Contains a daily review of your main metrics and the most important signals of the day.</p>
                </div>
                <div class="clear"></div>

                <!--<div class="email-alert-select">
                    <input id="real_time" class="email-notification-checkbox edit-notification" type="checkbox">
                    <label for="real_time">Real-time Signals</label>
                    <div class="clear"></div>
                    <p class="help-block">Get emails as we detect new signals. Never miss a beat.</p>
                </div>
                <div class="clear"></div>-->

            </div>
            <h5 class="action-nav-title"><strong>MENTIONS EMAIL ALERTS</strong></h5>
            <p class="settings-text">You will receive an email each time we collect a new mention for the streams you add below. Beware: pick a high volume stream and your inbox may get flooded. Use with caution.</p>
            <div class="mozaic-widget widget-keyword_subscriber" data-widget="keyword_subscriber" >
                <div class="keyword-subscription-search-box">
                    <select multiple id="streams">
                        <?php
                        $streamData = Stream::listStream(0);
                        $streamIds = explode(';', $streamData['streamid']);
                        $streamNames = explode(';', $streamData['streamname']);
                        for ($i = 0; $i < count($streamIds); $i++) :
                            ?>
                            <option class="type-keyword" value="<?php echo base64_encode($streamIds[$i]); ?>">
                            <?php echo $streamNames[$i] ?>
                            </option>
<?php endfor; ?>
                    </select>
                </div>
                <div class="clear"></div>

                <p class="mozaic-widget settings-text widget-keyword_subscriber_status" data-widget="keyword_subscriber_status" >
                    Remove each email alert by clicking the "x" next to it. <a class="clear-subscriptions btn btn-small"><i class="icon-clear"></i>Remove all Mentions Email Alerts</a>
                </p>
            </div>
        </div>
    </div>
</div>
<script>
    $.get('/ws/getoptions/{"name": "subscribed_streams", "type": "array"}', function(data) {
        $('#streams').select2('val', JSON.parse(data));
    });
    $("#streams").select2({
        formatResult: format,
        formatSelection: format,
        escapeMarkup: function(m) {
            return m;
        }
    });
    $('.clear-subscriptions').click(function() {
        $('#streams').select2('val', '');
        var filter = {};
        filter.name = 'subscribed_streams';
        filter.type = 'array';
        filter.value = $('#streams').select2('val');
        $.post('/ws/changeoptions/' + JSON.stringify(filter), function(data) {

        });
        return false;
    });
    function format(state) {
        if (!state.id)
            return state.text; // optgroup
        return '<i class="icon-stream-type icon-stream-company-small"/>' + state.text;
    }

    $('#streams').change(function(e) {
        var filter = {};
        filter.name = 'subscribed_streams';
        filter.type = 'array';
        filter.value = $(this).select2('val');
        $.post('/ws/changeoptions/' + JSON.stringify(filter), function(data) {
            console.log(JSON.parse(data));
        });
    });

    $('#daily_digest').change(function(e) {
        var filter = {};
        filter.name = 'daily_digest_email';
        filter.type = 'int';
        filter.value = 0;
        if ($(this).is(':checked'))
            filter.value = 1;
        $.post('/ws/changeoptions/' + JSON.stringify(filter), function(data) {
        });
    });
</script>